﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;

namespace SEVN.Web.Framework
{
    public partial class QuickOption : System.Web.UI.Page
    {
        // WebFrameDataContext db = new WebFrameDataContext();
        public static bool HasRight = true;
        public static bool HasLeft = false;
        public static MyPage CurrentPage = new MyPage();
        public static Skin CurrentSkin = new Skin();
        public static bool IsSelectedPage = true;

        protected void Page_Load(object sender, EventArgs e)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                if (CurrentPage.PageID < 1)
                    CurrentPage = db.MyPages.SingleOrDefault(p => p.PageName == "Default.aspx");

                if (CurrentPage != null)
                {
                    if (CurrentSkin.SkinID < 1)
                        CurrentSkin = CurrentPage.Skin;

                    SetPosition(CurrentSkin);
                    LoadAllControlToSkinForAdmin();

                    Ultility.LoadAllControlToPageForAdmin(this, placeContent, CurrentPage, 'C');

                    imgSkinDemo.ImageUrl = "~/" + CurrentSkin.Image;
                }

                if (!IsPostBack)
                {
                    cboPages.DataSource = db.MyPages;
                    cboPages.DataBind();

                    cboSkins.DataSource = db.Skins;
                    cboSkins.DataBind();

                    cboSkins.SelectedValue = CurrentPage.SkinID.ToString();

                    LoadDataToPageTab();

                    cboSkinTab.DataSource = db.Skins;
                    cboSkinTab.DataBind();



                    //MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);

                }
            }
        }

        protected void lbtLogin_Click(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, GetType(), "loadDefault", "<script type='text/javascript'>loadDynamic('Index.aspx')</script>", false);
        }

        protected void lbtLogout_Click(object sender, EventArgs e)
        {

        }


        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                using (WebFrameDataContext db = new WebFrameDataContext())
                {
                    MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);
                    if (pg != null)
                    {
                        PageControl pc = new PageControl();
                        pc.PageID = pg.PageID;
                        pc.ControlID = int.Parse(cboControls.SelectedValue);
                        pc.Position = 'C';
                        int maxOrder = Ultility.GetMaxOrder(pc.MyPage, pc.Position.Value);
                        pc.ControlOrder = maxOrder + 1;

                        db.PageControls.InsertOnSubmit(pc);
                        db.SubmitChanges();

                        placeContent.Controls.Clear();
                        Ultility.LoadAllControlToPageForAdmin(this, placeContent, pg, 'C');
                    }

                }
            }
            catch
            {

                ClientScript.RegisterStartupScript(GetType(), "Warring", "<script type='text/javascript'>alert('Có lỗi phát sinh trong khi thêm Control vào Page!')</script>");
            }

        }

        protected void cboSkins_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                Skin sk = db.Skins.SingleOrDefault(s => s.SkinID.ToString() == cboSkins.SelectedValue);

                if (sk != null)
                {
                    pnlSkin.Visible = true;
                    imgSkin.ImageUrl = "~/" + sk.Image;
                }
            }
        }

        protected void cboPages_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {

                MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);

                if (pg != null)
                {
                    CurrentPage = pg;
                    CurrentSkin = CurrentPage.Skin;

                    cboSkins.SelectedValue = pg.SkinID.ToString();
                    List<MyControl> listControl = Ultility.GetAvailableControlToList(pg);

                    cboControls.DataSource = listControl;
                    cboControls.DataBind();

                    SetPosition(pg.Skin);

                    LoadAllControlToSkinForAdmin();
                    Ultility.LoadAllControlToPageForAdmin(this, placeContent, pg, 'C');

                }
            }
        }

        private void SetPosition(Skin sk)
        {
            // Load Preview
            if (sk.Position.Contains('L'))
            {
                HasLeft = true;
            }
            else
                HasLeft = false;

            if (sk.Position.Contains('R'))
                HasRight = true;
            else
                HasRight = false;

            if (HasLeft && HasRight)
            {
                pnlLeft.CssClass = "left2 panels";
                pnlContent.CssClass = "content2";
                pnlRight.CssClass = "right2 panels";
            }
            else
            {
                pnlLeft.CssClass = "left panels";
                pnlContent.CssClass = "content";
                pnlRight.CssClass = "right panels";
            }
        }

        protected void lblCloseSkin_Click(object sender, EventArgs e)
        {
            pnlSkin.Visible = false;
        }

        protected void chkSelected_CheckedChanged(object sender, EventArgs e)
        {
            if (chkSelected.Checked)
            {
                pnlSelectSkin.Enabled = true;
            }
            else
            {
                pnlSelectSkin.Enabled = false;
            }
        }

        protected void btnApply_Click(object sender, EventArgs e)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {

                if (chkSelected.Checked && chkAccept.Checked)
                {
                    MyPage pg = db.MyPages.SingleOrDefault(p => p.PageID.ToString() == cboPages.SelectedValue);
                    if (pg != null)
                    {
                        pg.SkinID = int.Parse(cboSkins.SelectedValue);
                        db.SubmitChanges();

                        cboPages_SelectedIndexChanged(sender, e);

                        chkSelected.Checked = false;
                        pnlSelectSkin.Enabled = false;
                        chkAccept.Checked = false;
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "Warring", "<script type='text/javascript'>alert('Thao tác này chỉ có tác dụng khi bạn thay đổi Skin')</script>");
                    Ultility.LoadAllControlToPageForAdmin(this, placeContent, CurrentPage, 'C');
                }
            }
        }

        protected void chkAccept_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAccept.Checked)
                btnApply.Enabled = true;
            else
                btnApply.Enabled = false;
        }

        protected void cboControls_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void lbtSetupSkin_Click(object sender, EventArgs e)
        {

        }

        protected void lbtPages_Click(object sender, EventArgs e)
        {
            lbtPages.CssClass = "selectedTab";
            lbtSkins.CssClass = "tabControl";
            tabPage.Visible = true;
            tabSkin.Visible = false;

            IsSelectedPage = true;

            LoadAllControlToSkinForAdmin();
            Ultility.LoadAllControlToPageForAdmin(this, placeContent, CurrentPage, 'C');
        }

        protected void lbtSkins_Click(object sender, EventArgs e)
        {
            lbtSkins.CssClass = "selectedTab";
            lbtPages.CssClass = "tabControl";

            tabPage.Visible = false;
            tabSkin.Visible = true;

            IsSelectedPage = false;

            placeContent.Controls.Clear();

            // LoadAllControlToSkin();
            LoadDataToSkinTab(); // Load các Position, Module va Control

            cboSkinTab.SelectedValue = CurrentSkin.SkinID.ToString();
        }



        //private void LoadAllControlToSkin()
        //{
        //    placeHead.Controls.Clear();
        //    placeContent.Controls.Clear();
        //    placeLeft.Controls.Clear();
        //    placeRight.Controls.Clear();
        //    placeFooter.Controls.Clear();

        //    Ultility.LoadAllControlToSkin(this, placeHead, CurrentSkin, 'T');
        //    if (HasLeft)
        //        Ultility.LoadAllControlToSkin(this, placeLeft, CurrentSkin, 'L');
        //    if (HasRight)
        //        Ultility.LoadAllControlToSkin(this, placeRight, CurrentSkin, 'R');
        //    Ultility.LoadAllControlToSkin(this, placeContent, CurrentSkin, 'C');
        //    Ultility.LoadAllControlToSkin(this, placeFooter, CurrentSkin, 'B');
        //}

        private void LoadAllControlToSkinForAdmin()
        {
            placeHead.Controls.Clear();
            placeContent.Controls.Clear();
            placeLeft.Controls.Clear();
            placeRight.Controls.Clear();
            placeFooter.Controls.Clear();

            Ultility.LoadAllControlToSkinForAdmin(this, placeHead, CurrentSkin, 'T');
            if (HasLeft)
                Ultility.LoadAllControlToSkinForAdmin(this, placeLeft, CurrentSkin, 'L');
            if (HasRight)
                Ultility.LoadAllControlToSkinForAdmin(this, placeRight, CurrentSkin, 'R');

            //if (IsSelectedPage)
            //    Ultility.LoadAllControlToSkinForAdmin(this, placeContent, CurrentSkin, 'C');

            Ultility.LoadAllControlToSkinForAdmin(this, placeFooter, CurrentSkin, 'B');
        }

        protected void btnAddToSkin_Click(object sender, EventArgs e)
        {
            try
            {
                using (WebFrameDataContext db = new WebFrameDataContext())
                {
                    if (CurrentSkin != null)
                    {
                        SkinControl sc = new SkinControl();
                        sc.SkinID = int.Parse(cboSkinTab.SelectedValue);
                        sc.ControlID = int.Parse(cboControlSkinTab.SelectedValue);

                        sc.Position = Char.Parse(cboPosition.SelectedValue);
                        int maxOrder = Ultility.GetMaxOrder(CurrentSkin, sc.Position.Value);

                        sc.ControlOrder = maxOrder + 1;

                        db.SkinControls.InsertOnSubmit(sc);
                        db.SubmitChanges();

                        CurrentSkin.SkinControls.Add(sc);
                        LoadAllControlToSkinForAdmin();
                    }
                }
            }
            catch
            {

                ClientScript.RegisterStartupScript(GetType(), "Warring", "<script type='text/javascript'>alert('Có lỗi phát sinh trong khi thêm Control vào Skin!')</script>");
            }
        }

        protected void lbtCloseSkin_Click(object sender, EventArgs e)
        {
            pnlSkin.Visible = false;
        }
        [WebMethod]
        public static void ReorderControl(string controlCode, string parentNew, string parentOld, string order)
        {
            char oldPosition = Ultility.GetPosition(parentOld);
            char newPosition = Ultility.GetPosition(parentNew);

            int newOrder = -1;
            int.TryParse(order, out newOrder);

            newOrder += 1;

            if (newPosition == 'C')
            {
                Ultility.ReorderControlOnPage(CurrentPage, controlCode, newOrder);
            }
            else
                Ultility.ReorderControlOnSkin(CurrentSkin, controlCode, oldPosition, newPosition, newOrder);

        }
        static string _location = "";
        [WebMethod(EnableSession = true)]
        [ScriptMethod()]
        public static void saveStyle(string location)
        {
            _location = location;
        }
        [WebMethod(EnableSession = true)]
        [ScriptMethod()]
        public static void RemoveControlOnPage(string controlCode, string parentOld, int length)
        {
            char oldPosition = Ultility.GetPosition(parentOld);

            if (oldPosition == 'C')
            {
                Ultility.DeleteControl(CurrentPage, controlCode, oldPosition, length);
            }
            else
                Ultility.DeleteControl(CurrentSkin, controlCode, oldPosition, length);
        }
        protected void btnSaveEditCSS_Click(object sender, ImageClickEventArgs e)
        {
            //http://localhost:9189/SEVN%20Framework
            _location = _location.Substring(39);
            System.IO.File.WriteAllText(this.MapPath(_location), txtViewCSS.Text);
        }

        protected void btnSaveEditCSS_Click1(object sender, ImageClickEventArgs e)
        {
            System.IO.File.WriteAllText(this.MapPath("Styles/" + _location), txtViewCSS.Text);
        }


        protected void cboSkinTab_SelectedIndexChanged(object sender, EventArgs e)
        {
            using (WebFrameDataContext db = new WebFrameDataContext())
            {
                Skin sk = db.Skins.SingleOrDefault(s => s.SkinID.ToString() == cboSkinTab.SelectedValue);

                if (sk != null)
                {
                    CurrentSkin = sk;

                    if (sk != null)
                    {
                        //pnlImageSkin.Visible = true;
                        imgSkinDemo.ImageUrl = "~/" + sk.Image;
                    }

                    LoadDataToSkinTab(); // Load các Position, Module va Control

                    SetPosition(sk);
                    LoadAllControlToSkinForAdmin();
                }
            }

        }

        private void LoadDataToSkinTab()
        {
            cboPosition.DataSource = Ultility.GetPositionsBySkin(CurrentSkin);
            cboPosition.DataBind();

            cboSkinModules.DataSource = Ultility.GetModulesByPosition(char.Parse(cboPosition.SelectedValue));
            cboSkinModules.DataBind();

            cboControlSkinTab.DataSource = Ultility.GetAvailableControlToListByModule(CurrentSkin, int.Parse(cboSkinModules.SelectedValue), char.Parse(cboPosition.SelectedValue));
            cboControlSkinTab.DataBind();
        }

        private void LoadDataToPageTab()
        {

            cboModules.DataSource = Ultility.GetModulesByPosition('C');
            cboModules.DataBind();

            cboControls.DataSource = Ultility.GetAvailableControlToPageByModule(CurrentPage, int.Parse(cboModules.SelectedValue));
            cboControls.DataBind();
        }

        protected void cboPosition_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboSkinModules.DataSource = Ultility.GetModulesByPosition(char.Parse(cboPosition.SelectedValue));
            cboSkinModules.DataBind();

            cboControlSkinTab.DataSource = Ultility.GetAvailableControlToListByModule(CurrentSkin, int.Parse(cboSkinModules.SelectedValue), char.Parse(cboPosition.SelectedValue));
            cboControlSkinTab.DataBind();

            SetPosition(CurrentSkin);
            LoadAllControlToSkinForAdmin();
        }

        protected void cboModules_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboControls.DataSource = Ultility.GetAvailableControlToPageByModule(CurrentPage, int.Parse(cboModules.SelectedValue));
            cboControls.DataBind();
        }

        protected void cboSkinModules_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboControlSkinTab.DataSource = Ultility.GetAvailableControlToListByModule(CurrentSkin, int.Parse(cboSkinModules.SelectedValue), char.Parse(cboPosition.SelectedValue));
            cboControlSkinTab.DataBind();

            SetPosition(CurrentSkin);
            LoadAllControlToSkinForAdmin();
        }

        protected void cboControlSkinTab_SelectedIndexChanged(object sender, EventArgs e)
        {
            SetPosition(CurrentSkin);
            LoadAllControlToSkinForAdmin();
        }
    }
}